import { Gird, PointType } from "./Gird";
import { Search } from "./Search";


const {ccclass, property} = cc._decorator;

@ccclass
export default class Main extends cc.Component {

    private _griaph: cc.Graphics = null;

    private _gird: Gird = null;

    public get Gird (){
        return this._gird;
    }
    private _search: Search = null;

    private isMouseDown: boolean = false;

    private clickBlockState: PointType = PointType.normalgird;
    onLoad () {
        
        this._griaph = this.node.addComponent(cc.Graphics);
        this._gird = new Gird( -360, -220, 20, 30, 20 );
        this.drawGrid();
        this.listenON();
        this._search = new Search( this );
    }

    start () {

        let btns = this.node.getChildByName("btns").children;
        btns.forEach((btn)=>{
            btn.on( cc.Node.EventType.TOUCH_END, this.onbtnHalder, this);
        })
    }

    /** 绘制界面格子 */
    private drawGrid () {

        let blockArray = this._gird.blockArray;
        for( let i = 0; i < blockArray.length; i++){
            let block = blockArray[i];
            this._griaph.rect( block._x,block._y,block._side, block._side);
            this._griaph.fillColor = block._color;
            this._griaph.fill();
        }
    }

    /** 事件监听 */
    private listenON () {
        
        this.node.on(cc.Node.EventType.MOUSE_DOWN,this.onMouseDown,this);
        this.node.on (cc.Node.EventType.MOUSE_MOVE,this.onMouseMove,this);
        this.node.on ( cc.Node.EventType.MOUSE_UP,this.onMouseUp,this);
    }
    
    private listenOFF (){
        
        this.node.off(cc.Node.EventType.MOUSE_DOWN,this.onMouseDown,this);
        this.node.off(cc.Node.EventType.MOUSE_MOVE,this.onMouseMove,this);
        this.node.off( cc.Node.EventType.MOUSE_UP,this.onMouseUp,this);
    }

    private onMouseDown ( event: cc.Event.EventTouch ) {
        
        let pos = this.node.convertToNodeSpaceAR( event.getLocation() );
        let data = this._gird.checkPositionINgird( pos );
        this.clickBlockState = data.type;
        this.isMouseDown = true;
    }

    private onMouseMove ( event: cc.Event.EventTouch ) {
        
        if( !this.isMouseDown ){
            return;
        }
        let pos = this.node.convertToNodeSpaceAR( event.getLocation() ); 
        let pointType = this._gird.checkPositionINgird( pos );
        if( this.clickBlockState == PointType.normalgird && pointType.type == PointType.normalgird ){
            this._gird.setWall(pointType.index);
            this.drawGrid();
        }
    }

    private onMouseUp ( event: cc.Event.EventTouch ) {
        
        let pos = this.node.convertToNodeSpaceAR( event.getLocation() ); 
        let pointType = this._gird.checkPositionINgird( pos );
        if( this.clickBlockState == PointType.startgird || this.clickBlockState == PointType.end_gird ){
            
            this._gird.changePoint( this.clickBlockState, pointType.index );
            this.drawGrid();
        }else if(this.clickBlockState == PointType.normalgird){
            this._gird.setWall(pointType.index);
            this.drawGrid();
        }
        this.isMouseDown = false;
    }

    private onbtnHalder ( event: cc.Event.EventTouch ){
        
        let name = event.target.name;
        cc.log(name);
        switch (name) {
            case "btn_step":{

                this.stepfindpath();
                break;
            }
            case "btn_auto":{

                break;
            }
            case "btn_drawpath":{

                break;
            }
            case "btn_actpath":{

                break;
            }
            case "btn_reset":{

                break;
            }  
        
            default:
                break;
        }
    }

    public stepfindpath (){

        if(!this._search.startpos){
            this._search.startpos = this._gird.startBlock;
        }
        let complete = ()=>{
            this.drawGrid();
        }
        this._search.stepfindpath( complete );
    }

    onDestroy(){
        this.listenOFF();
    }
}
